let WX = require('WX')
let MiniP = cc.Class({
  extends: cc.Component,

  statics: {
    instance: null
  },

  ctor () {
    if (!MiniP.instance) {
      MiniP.instance = this
    }
  },

  properties: {},

  onLoad () {
    if (MiniP.instance == this) {
      cc.game.addPersistRootNode(this.node)
    } else {
      this.node.removeFromParent(true)
    }
  },

  start () {},

  getMiniPNavigatorPos (level) {
    let miniPNavigatorPos = [
      {
        // 0
        x: [-350, 350],
        y: [-700, -700]
      },
      {
        // 1
        x: [0,0],
        y: [550,-400]
      },
      {
        // 2
        x: [-300,-300],
        y: [500,-500]
      },
      {
        // 3
        x: [-400,400],
        y: [-100,-100]
      },
      {
        // 4
        x: [400,400],
        y: [200,600]
      },
      {
        // 5
        x: [-200,-200],
        y: [500,-600]
      },
      {
        // 6
        x: [-400,400],
        y: [-600,-600]
      },
      {
        // 7
        x: [-400,400],
        y: [-400,-400]
      },
      {
        // 8
        x: [0,0],
        y: [500,-600]
      },
      {
        // 9
        x: [-300,-300],
        y: [500,-600]
      },
      {
        // 10
        x: [-400,400],
        y: [-300,-300]
      },
      {
        // 11
        x: [400,400],
        y: [200,600]
      },
      {
        // 12
        x: [400,-400],
        y: [0,0]
      },
      {
        // 13
        x: [-400,400],
        y: [-300,-300]
      },
      {
        // 14
        x: [-400,400],
        y: [-300,-300]
      },
      {
        // 15
        x: [400,400],
        y: [600,-600]
      },
      {
        // 16
        x: [-450,450],
        y: [-400,-400]
      },
      {
        // 17
        x: [0,0],
        y: [600,-500]
      },
      {
        // 18
        x: [-400,400],
        y: [-500,-500]
      },
      {
        // 19
        x: [400,400],
        y: [600,-500]
      },
      {
        // 20
        x: [-300,300],
        y: [-600,-600]
      },
      {
        // 21
        x: [-450,450],
        y: [-500,-500]
      },
      {
        // 22
        x: [-400,400],
        y: [-400,-400]
      },
      {
        // 23
        x: [-400,400],
        y: [500,500]
      },
      {
        // 24
        x: [-400,400],
        y: [-600,-600]
      },
      {
        // 25
        x: [-400,400],
        y: [-600,-600]
      },
      {
        // 26
        x: [100,400],
        y: [-600,-600]
      },
      {
        // 27
        x: [-300,300],
        y: [500,-500]
      },
      {
        // 28
        x: [-400,400],
        y: [0,0]
      },
      {
        // 29
        x: [-450,450],
        y: [200,200]
      },
      {
        // 30
        x: [-400,400],
        y: [-600,-600]
      },
      {
        // 31
        x: [0,0],
        y: [600,-600]
      },
      {
        // 32
        x: [400,400],
        y: [200,500]
      },
      {
        // 33
        x: [-300,-300],
        y: [200,500]
      },
      {
        // 34 to 0
        x: [],
        y: []
      },
      {
        // 35
        x: [400,400],
        y: [-600,600]
      },
      {
        // 36
        x: [400,400],
        y: [600,-600]
      },
      {
        // 37
        x: [-400,400],
        y: [-500,-500]
      },
      {
        // 38
        x: [-430,430],
        y: [-500,-500]
      },
      {
        // 39
        x: [-400,-100],
        y: [-700,-700]
      },
      {
        // 40
        x: [-440,440],
        y: [0,0]
      }
    ]
    return miniPNavigatorPos[level]
  },

  init () {
    let positions = [
      { x: -300, y: 526 },
      { x: -70, y: 526 },
      { x: 160, y: 526 },
      { x: 390, y: 526 },
      { x: -300, y: -320 },
      { x: -70, y: -320 },
      { x: 160, y: -320 },
      { x: 390, y: -320 }
    ];
    this.node.parent.getChildByName("bgs").active = true;
    let minips = this.node.children
    for (let i = 0; i < minips.length; i++) {
      minips[i].active = true
      minips[i].x = positions[i].x
      minips[i].y = positions[i].y
      minips[i].stopAllActions()
      minips[i].runAction(
        cc.repeatForever(cc.sequence(cc.scaleTo(1, 1.1), cc.scaleTo(1, 1)))
      )
    }
  },

  inBoard (level) {
    this.node.parent.getChildByName("bgs").active = false;
    let miniPNavigatorPos = this.getMiniPNavigatorPos(level)
    console.log("MiniP-inBoard miniPNavigatorPos",miniPNavigatorPos);
    let minips = this.node.children
    for (let p of minips) p.active = false
    let index = Math.floor(Math.random() * (minips.length - 1))
    for (let i = 0; i <= 1; i++) {
      minips[index + i].active = true
      minips[index + i].x = miniPNavigatorPos.x[i]
      minips[index + i].y = miniPNavigatorPos.y[i]
    }
  },

  navigateToMiniProgram1 () {
    // 雪人
    WX.navigateToMiniP('wxe705798aa087b393')
  },

  navigateToMiniProgram2 () {
    // 漂移停车
    WX.navigateToMiniP('wx6ad0a2b9f0b1cca5')
  },

  navigateToMiniProgram3 () {
    // 后宫
    WX.navigateToMiniP('wxef79df78bf8a7cc4')
  },

  navigateToMiniProgram4 () {
    // 跑酷
    WX.navigateToMiniP('wxe2bf31157b6527fa')
  },
  navigateToMiniProgram5 () {
    // 贪吃蛇
    WX.navigateToMiniP('wx34e5f5bbe0a08d6e')
  },
  navigateToMiniProgram6 () {
    // 龙卷风
    WX.navigateToMiniP('wx797292f4684f3b42')
  },
  navigateToMiniProgram7 () {
    // 射箭
    WX.navigateToMiniP('wxe879e64c7647d6ef')
  },

  update (dt) {}
})
